package cn.edu.xaut.mybatisers.mall.tool;

import cn.edu.xaut.mybatisers.mall.Dao.AdminDao;
import cn.edu.xaut.mybatisers.mall.Dao.BillDao;
import cn.edu.xaut.mybatisers.mall.Entity.Bill;
import cn.edu.xaut.mybatisers.mall.Entity.Commodity;
import cn.edu.xaut.mybatisers.mall.Exception.GlobalExceptionAdvice;
import io.swagger.annotations.ApiOperation;
import org.apache.log4j.Logger;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Map;

@Component
@EnableRabbit
@Configuration
@SuppressWarnings("all")
public class DelayReceive {
    @Autowired
    BillDao billDao;

    @Autowired
    AdminDao adminDao;

    private static Logger logger = Logger.getLogger(DelayReceive.class);
    @ApiOperation("未支付的订单超时，设置为取消状态")
    @RabbitListener(queues = "billDelayQueue")
    public void processBillStatus(String orderNumber) throws SQLException {
        // 测试把之前推送的消息给消费下来了
        System.out.println("长时间未支付, 更改订单状态: 订单号【" + orderNumber + "】  时间【" + LocalDateTime.now() + "】");
        Bill bill = new Bill();
        bill.setOrder_number(orderNumber);
        bill.setBillStatus(6);
        if (billDao.selectByOrderNumber(orderNumber).size() > 0) {
            billDao.alterOrder(bill);
            logger.info("执行定时任务, 更改订单 {订单号 ==>> [%s}] 的状态为取消订单".format(orderNumber));
        }else
            logger.info("未执行定时任务，未更改订单 {订单号 ==>> [%s}] 的状态为取消订单".format(orderNumber));
        bill = null;
    }

    @ApiOperation("将新加的商品到期后，设置为不是新商品")
    @RabbitListener(queues = "hotCommodityDelayQueue")
    public void processHotCommodity(int commodity_id) throws SQLException {
        System.out.println("新品时间过期, 更改新品状态: 商品id【" + commodity_id + "】  时间【" + LocalDateTime.now() + "】");
        Commodity commodity = new Commodity();
        commodity.setId(commodity_id);
        commodity.setIsNew("0");
        adminDao.alterCommodity(commodity);
        commodity = null;
    }
}
